package com.ikun.kunpicturebackend.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ikun.kunpicturebackend.annotation.AuthCheck;
import com.ikun.kunpicturebackend.common.BaseResponse;
import com.ikun.kunpicturebackend.common.DeleteRequest;
import com.ikun.kunpicturebackend.common.ResultUtils;
import com.ikun.kunpicturebackend.domain.User;
import com.ikun.kunpicturebackend.exception.ErrorCode;
import com.ikun.kunpicturebackend.exception.ThrowUtils;
import com.ikun.kunpicturebackend.model.dto.user.*;
import com.ikun.kunpicturebackend.model.vo.LoginUserVO;
import com.ikun.kunpicturebackend.model.vo.UserVO;
import com.ikun.kunpicturebackend.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     * @param registerRequest 注册参数
     * @return
     */
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest registerRequest) {
        ThrowUtils.throwIf(registerRequest == null, ErrorCode.PARAMS_ERROR);
        String userAccount = registerRequest.getUserAccount();
        String userPassword = registerRequest.getUserPassword();
        String checkPassword = registerRequest.getCheckPassword();

        long result = userService.userRegister(userAccount, userPassword, checkPassword);
        return ResultUtils.success(result);
    }

    /**
     * 用户登录
     * @param loginRequest 登录请求
     * @param request
     * @return
     */
    @PostMapping("/login")
    public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest loginRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(loginRequest == null, ErrorCode.PARAMS_ERROR);
        String userAccount = loginRequest.getUserAccount();
        String userPassword = loginRequest.getUserPassword();
        LoginUserVO loginUserVO = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(loginUserVO);
    }

    /**
     * 获取登录信息
     * @param request
     * @return
     */
    @GetMapping("/get/login")
    public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(BeanUtil.copyProperties(loginUser, LoginUserVO.class));
    }

    /**
     * 用户注销
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogOut(HttpServletRequest request) {
        Boolean result = userService.userLogOut(request);
        return ResultUtils.success(result);
    }

    /**
     * 新增用户【管理员】
     * @param userAddRequest
     * @return
     */
    @PostMapping("/add")
    @AuthCheck
    public BaseResponse<Long> addUser(@RequestBody UserAddRequest userAddRequest){
        ThrowUtils.throwIf(userAddRequest == null, ErrorCode.PARAMS_ERROR);
        //属性拷贝
        User user = BeanUtil.copyProperties(userAddRequest, User.class);
        //设置默认密码
        final String password = "12345678";
        String encryptPassword = userService.getEncryptPassword(password);
        user.setUserPassword(encryptPassword);
        //保存用户
        boolean result = userService.save(user);
        ThrowUtils.throwIf(!result, ErrorCode.SYSTEM_ERROR);
        return ResultUtils.success(user.getId());
    }

    /**
     * 根据id获取用户未脱敏 【管理员】
     * @param id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck
    public BaseResponse<User> getUserById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        User user = userService.getById(id);
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(user);
    }

    /**
     * 根据id获取用户 脱敏
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<UserVO> getUserVOById(long id) {
        BaseResponse<User> user = getUserById(id);
        User data = user.getData();
        return ResultUtils.success(BeanUtil.copyProperties(data, UserVO.class));
    }

    /**
     * 根据id删除用户 【管理员】
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest request) {
        ThrowUtils.throwIf(request == null || request.getId() <= 0, ErrorCode.PARAMS_ERROR);
        boolean b = userService.removeById(request.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新用户 【管理员】
     * @param request
     * @return
     */
    @PostMapping("/update")
    @AuthCheck
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest request) {
        ThrowUtils.throwIf(request == null || request.getId() == null, ErrorCode.PARAMS_ERROR);
        User user = BeanUtil.copyProperties(request, User.class);
        boolean result = userService.updateById(user);
        ThrowUtils.throwIf(!result, ErrorCode.SYSTEM_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 分页获取用户列表 脱敏信息 【管理员】
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    @AuthCheck
    public BaseResponse<Page<UserVO>> listUserVOByPage(@RequestBody UserQueryRequest request){
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        //当前页码和每页大小
        int current = request.getCurrent();
        int pageSize = request.getPageSize();
        Page<User> page = userService.page(new Page<>(current, pageSize), userService.getQueryWrapper(request));
        ThrowUtils.throwIf(page == null, ErrorCode.NOT_FOUND_ERROR);
        Page<UserVO> result = new Page<>(current, pageSize);
        BeanUtil.copyProperties(page, result);
        return ResultUtils.success(result);
    }
}
